﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Serialization;


namespace Order
{
    [Serializable]
    public class OrderService
    {
        public List<Order> list = new List<Order>();

        public OrderService(List<Order> orderlist)
        {
            foreach (Order temp in orderlist)
            {
                list.Add(temp);
            }
        }
        public OrderService() { }


        public void DeleteOrder(int ID)
        {
            int k = 0;
            while (list[k].OrderID != ID)
            {
                if (k < list.ToArray().Length - 1)
                    k++;
                else
                {
                    Console.WriteLine("不存在这个订单！！！");
                    throw new Exception("输入错误！！！");
                    return;
                }
            }
            list.Remove(list[k]);
        }


        public void AddOrder(object c)
        {
            Order m = c as Order;
            if (m == null)
            {
                Console.WriteLine("这不是一个订单！！！");
                throw new Exception("输入错误！！！");
            }
            else list.Add(m);
        }


        public List<Order> SearchOrder(int ID)
        {
            var query = from order in list
                        where ID == order.OrderID
                        orderby order.Sum
                        select order;
            List<Order> result = query.ToList();
            return result;
        }


        public List<Order> SearchOrder(string kind,string name)
        {
            if (kind == "CustomerID")
            {
                var query = from order in list
                            where name == order.CustomerID
                            orderby order.Sum
                            select order;
                List<Order> result = query.ToList();
                return result;
            }
            else if (kind == "ProductID")
            {
                var query = from order in list
                            where (from orderdeteil in order.OrderDetailsList
                                  where orderdeteil.ProductID==name
                                  select order.OrderID).Contains(order.OrderID)
                            orderby order.Sum
                            select order;
                List<Order> result = query.ToList();
                return result;
            }
            else return null;
        }


        public void ChangeOrder(int ID)
        {
            int k = 0;
            while (list[k].OrderID != ID)
            {
                if (k < list.ToArray().Length-1)
                    k++;
                else
                {
                    Console.WriteLine("不存在这个订单！！！");
                    throw new Exception("输入错误！！！");
                    return;
                }
            }
                Console.Write("是否修改订单ID（Y/N）：");
                string ch = Console.ReadLine();
                if (ch == "Y")
                {
                    Console.Write("输入修改后的订单号：");
                    int x = Convert.ToInt32(Console.ReadLine());
                    foreach (Order temp2 in list)
                    {
                        if (temp2.OrderID != x) continue;
                        else
                        {
                            Console.WriteLine("已存在这个订单！！！");
                            throw new Exception("输入错误！！！");
                            return;
                        }
                    }
                    list[k].OrderID = x;
                }
                else if (ch == "N") Console.WriteLine("继续修改...");

                Console.Write("是否修改客户ID（Y/N）：");
                ch = Console.ReadLine();
                if (ch == "Y")
                    {
                        Console.Write("输入修改后的顾客ID：");
                        string y = Console.ReadLine();
                        list[k].CustomerID = y;
                    }
                else if (ch == "N") Console.WriteLine("继续修改...");

            Console.Write("是否修改订单明细（Y/N）：");
                ch = Console.ReadLine();
            if (ch == "N")
            {
                Console.WriteLine("订单修改完成！");
                return;
            }
            else if (ch == "Y")
            {
                for (int i = 0; i < list[k].OrderDetailsList.ToArray().Length; i++)
                {
                    Console.WriteLine("第" + (i + 1) + "个订单明细：");

                    Console.Write("是否修改明细ID（Y/N）：");
                    string chr = Console.ReadLine();
                    if (chr == "Y")
                    {
                        Console.Write("输入修改后的明细ID：");
                        int x = Convert.ToInt32(Console.ReadLine());
                        list[k].OrderDetailsList[i].OrderdetailID = x;
                    }
                    else if (chr == "N") Console.WriteLine("继续修改...");

                    Console.Write("是否修改产品ID（Y/N）：");
                    chr = Console.ReadLine();
                    if (chr == "Y")
                    {
                        Console.Write("输入修改后的产品ID：");
                        string y = Console.ReadLine();
                        list[k].OrderDetailsList[i].ProductID = y;
                    }
                    else if (chr == "N") Console.WriteLine("继续修改...");

                    Console.Write("是否修改产品数量（Y/N）：");
                    chr = Console.ReadLine();
                    if (chr == "Y")
                    {
                        Console.Write("输入修改后的产品数量：");
                        int z = Convert.ToInt32(Console.ReadLine());
                        list[k].OrderDetailsList[i].ProductNumber = z;
                    }
                    else if (chr == "N") Console.WriteLine("继续修改...");

                    Console.Write("是否修改产品价格（Y/N）：");
                    chr = Console.ReadLine();
                    if (chr == "Y")
                    {
                        Console.Write("输入修改后的产品价格：");
                        int w = Convert.ToInt32(Console.ReadLine());
                        list[k].OrderDetailsList[i].ProductNumber = w;
                    }
                    else if (chr == "N") continue;
                }
                Console.WriteLine("明细修改完成！");
            } 
            Console.WriteLine("修改完成！");
        }


        public void Export(string xmlname)
        {
            XmlSerializer xmlserilizer = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream(xmlname, FileMode.Create))
            {
                xmlserilizer.Serialize(fs, this.list);
            }
        }


        public void ImPort(string xmlname)
        {
            XmlSerializer xmlserilizer = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream(xmlname, FileMode.Open))
            {
                this.list = (List<Order>)xmlserilizer.Deserialize(fs);
            }
        }

   }
}
